/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.editor.pipe;

import java.awt.BorderLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import twaver.Element;
import twaver.MovableFilter;
import twaver.ResizableFilter;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import twaver.tree.TTree;
import demo.DemoPane;
import demo.DemoUtil;
import demo.editor.EditablePopupMenuGenerator;
import demo.editor.pipe.element.AbstractPipe;
import demo.editor.pipe.element.RoundPipe;
import demo.editor.pipe.element.SquarePipe;
import demo.tree.TreePopupMenuGenerator;

public class PipeEditorDemo extends DemoPane {
    private TDataBox box = new TDataBox("Pipe Editor");
    private TNetwork network = new TNetwork(box);
    private TTree tree = new TTree(box);
    private TPropertySheet sheet = new TPropertySheet(box);

	public PipeEditorDemo(){
		JSplitPane leftSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(tree), new TPropertySheetPane(sheet));
		JSplitPane mainSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSplit, network);
		this.setLayout(new BorderLayout());
		this.add(mainSplit, BorderLayout.CENTER);
		this.add(DemoUtil.createStatusLabel(network), BorderLayout.SOUTH);
		leftSplit.setDividerLocation(300);
		mainSplit.setDividerLocation(200);

		// set network
		network.setToolbarByName("PipeEditorToolbar");

		final JCheckBox draggableAll = new JCheckBox("Draggable All", false);
		network.getToolbar().add(draggableAll);

		network.setEnsureVisibleOnSelected(true);
		network.setEnableDeleteWithKeyboard(true);
		network.setEnableCopyPasteWithKeyboard(true);
        network.setResizableFilter(new ResizableFilter(){
			public boolean isResizable(Element element) {
				if(draggableAll.isSelected()){
					return true;
				}
				return element instanceof AbstractPipe && ((AbstractPipe)element).getHost() == null;
			}
		});
        network.setPopupMenuGenerator(new EditablePopupMenuGenerator(network));
        network.clearMovableFilters();
        network.addMovableFilter(new MovableFilter(){
			public boolean isMovable(Element element) {
				if(draggableAll.isSelected()){
					return true;
				}
				return element instanceof AbstractPipe && ((AbstractPipe)element).getHost() == null;
			}
        });
        // set tree
        tree.setEnsureVisibleOnSelected(true);
        tree.setElementDraggable(true);
        tree.setPopupMenuGenerator(new TreePopupMenuGenerator(tree));

        // set propertySheet
        sheet.setEditable(true);

        // init box
        this.initDataBox();
	}

	private void initDataBox(){
        RoundPipe roundPipe = new RoundPipe();
        roundPipe.setHoleCount(20);
        roundPipe.setLocation(new Point(50, 15));
        roundPipe.setSize(230, 230);
        this.box.addElement(roundPipe);
        for (int i = 0; i < roundPipe.getHoleCount(); i++)
        {
            RoundPipe hole = new RoundPipe();
            hole.putLabelHighlightable(false);
            hole.putLabelPosition(TWaverConst.POSITION_CENTER);
            hole.setParent(roundPipe);
            hole.setHoleIndex(i);
            hole.setName("" + i);
            hole.setHost(roundPipe);
            this.box.addElement(hole);

            if (i == roundPipe.getHoleCount() - 1)
            {
                hole.setHoleCount(5);
                hole.setCenterHole(false);
                for (int j = 0; j < hole.getHoleCount(); j++)
                {
                    RoundPipe child = new RoundPipe();
                    child.putCustomDrawFillColor(TWaverUtil.getRandomColor());
                    child.putLabelHighlightable(false);
                    child.putLabelPosition(TWaverConst.POSITION_CENTER);
                    child.setParent(hole);
                    child.setHoleIndex(j);
                    child.setName("" + j);
                    child.setHost(hole);
                    this.box.addElement(child);
                }
            }
        }

        SquarePipe squarePipe = new SquarePipe();
        List cellCounts = new ArrayList();
        cellCounts.add(TWaverUtil.valueOf(3));
        cellCounts.add(TWaverUtil.valueOf(4));
        cellCounts.add(TWaverUtil.valueOf(5));
        cellCounts.add(TWaverUtil.valueOf(6));
        cellCounts.add(TWaverUtil.valueOf(5));
        cellCounts.add(TWaverUtil.valueOf(3));
        cellCounts.add(TWaverUtil.valueOf(3));
        squarePipe.setCellCounts(cellCounts);
        squarePipe.setLocation(new Point(30, 270));
        squarePipe.setSize(300, 200);
        this.box.addElement(squarePipe);
        for (int i = 0; i < squarePipe.getAllCellCount(); i++)
        {
            SquarePipe hole = new SquarePipe();
            if (i >= 12 && i <= 17)
            {
                hole.putCustomDrawFillColor(TWaverUtil.getRandomColor());
            }
            hole.putLabelHighlightable(false);
            hole.putLabelPosition(TWaverConst.POSITION_CENTER);
            hole.setParent(squarePipe);
            hole.setHoleIndex(i);
            hole.setName("" + i);
            hole.setHost(squarePipe);
            this.box.addElement(hole);
        }
	}
}